package com.ghxxzx.daglcustom.controller.task;

import com.ghxxzx.daglcustom.utils.ActivitiTools;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.tmsps.ne4spring.utils.ChkTools;

import java.util.List;
import java.util.Map;

@Controller
public class TodoTaskController {

	@Autowired
	private TaskService taskService;
	@Autowired
	private RuntimeService runtimeService;

	@RequestMapping("/todoTaskList")
	@ResponseBody
	public List<Map<String, Object>> todoTaskList(String name) {
		// 创建查询对象
		TaskQuery taskQuery = taskService.createTaskQuery();
		if (name != null && !"".equals(name)) {
			taskQuery.processDefinitionNameLike("%" + name + "%");
		}
		// 设置查询条件
		// 根据任务的办理人查询
		// taskQuery.taskAssignee(assignee);
		// 指定流程定义key，只查询某个流程的任务
		// taskQuery.processDefinitionKey(processDefinitionKey);
		// 获取查询列表
		List<Task> list = taskQuery.list();
		List<Map<String, Object>> listEnd = ActivitiTools.turnTasks(list);

		// 分页时用,非分页慎用
		/*
		 * ProcessInstanceQuery instanceQuery =
		 * runtimeService.createProcessInstanceQuery(); for (Map<String, Object> map :
		 * listEnd) { String processDefinitionId = (String)
		 * map.get("processDefinitionId"); if (processDefinitionId != null) {
		 * List<ProcessInstance> processInstanceList =
		 * instanceQuery.processDefinitionId(processDefinitionId).list(); if
		 * (processInstanceList != null) { for (ProcessInstance processInstance :
		 * processInstanceList) { map.put("processInstanceName",
		 * processInstance.getName()); } } } }
		 */

		return listEnd;
	}
	
	@RequestMapping("/mineTodoTaskList")
	@ResponseBody
	public List<Map<String, Object>> todoTaskList(String name, String user_id, String depart_id) {
		// 创建查询对象
		TaskQuery taskQuery = taskService.createTaskQuery();
		TaskQuery taskQuery1 = taskService.createTaskQuery();
		if (name != null && !"".equals(name)) {
			taskQuery.processDefinitionNameLike("%" + name + "%");
			taskQuery1.processDefinitionNameLike("%" + name + "%");
		}
		taskQuery.taskCandidateOrAssigned(user_id);
		taskQuery1.taskCandidateOrAssigned(depart_id);
		// 设置查询条件
		// 根据任务的办理人查询
		// taskQuery.taskAssignee(assignee);
		// 指定流程定义key，只查询某个流程的任务
		// taskQuery.processDefinitionKey(processDefinitionKey);
		// 获取查询列表
		List<Task> list = taskQuery.list();
		List<Task> list1 = taskQuery1.list();
		list.addAll(list1);
		List<Map<String, Object>> listEnd = ActivitiTools.turnTasks(list);
		return listEnd;
	}
	
	@RequestMapping("/claimAndComplete")
	@ResponseBody
	public ProcessInstance claimAndComplete(String taskId, String assignee, String result) {
		// 创建查询对象
		Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
		String pid = task.getProcessInstanceId();
		if(ChkTools.isNull(task.getAssignee())) {
			taskService.claim(taskId, assignee);
		}
		taskService.complete(taskId);
		if("审批拒绝".equals(result)) {
			runtimeService.deleteProcessInstance(pid, "审批拒绝");
		}
		// 获取流程实例
		ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(pid).singleResult();
		return pi;
	}
}